package puan.com.weighBox.activity;

import androidx.appcompat.app.AppCompatActivity;
import puan.com.weighBox.R;
import puan.com.weighBox.constants.Constants;
import puan.com.weighBox.presenter.LoginPresenter;
import puan.com.weighBox.viewInterface.ILoginView;
import puan.com.weighBox.widget.LoginButton;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.StringUtils;

public class LoginActivity extends AppCompatActivity implements ILoginView {

    private EditText etUserName, etPassword;
    private LoginButton btnLogin;
    private LoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
    }

    private void initData() {
        btnLogin.setButtonText("登录");
        presenter = new LoginPresenter(this);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnLogin.startAnim();
                String username = etUserName.getText().toString();
                String password = etPassword.getText().toString();
                String loginUser = presenter.checkLoginParamsIsLegal(username, password);

                handleLoginResult(loginUser);
            }
        });
    }

    private void handleLoginResult(String loginUser) {
        btnLogin.stopAnim();
        if (StringUtils.isEmpty(loginUser)) {
            return;
        }
        if (loginUser.equals(Constants.ADMIN_USERNAME)) {
            ActivityUtils.startActivity(new Intent(LoginActivity.this, AdminActivity.class));
        } else if (loginUser.equals(Constants.OPERATOR_USERNAME)) {
//            ActivityUtils.startActivity(new Intent(LoginActivity.this, OperatorActivity.class));
        }
        finish();
    }

    private void initView() {
        etUserName = findViewById(R.id.etUserName);
        etPassword = findViewById(R.id.etPassword);
        btnLogin = findViewById(R.id.btnLogin);
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
